package com.atcd.mq.mq_yanchimq.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg/{message}")
    public void sendMsg(@PathVariable String message) {

        System.out.println("当前事件:" + new Date().toString() + "," + "发送一条信息给两个 TTL  队列:" + message);

        rabbitTemplate.convertAndSend("X", "XA", " 消息来自 ttl 为 为 10S  的队列: " + message);
        rabbitTemplate.convertAndSend("X", "XB", " 消息来自 ttl 为 为 40S  的队列: " + message);
    }

    //优化后的生产者
    @GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
    public void sendMsg(@PathVariable String message, @PathVariable String ttlTime) {
        rabbitTemplate.convertAndSend("X", "XC", message, correlationData -> {
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });
        System.out.println("当前时间: " + new Date().toString() + "," + "发送一条信息给 QC  队列:" + message);
    }


    //基于插件的延迟消息
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    @GetMapping("sendDelayMsg/{message}/{delayTime}")
    public void sendMsg(@PathVariable String message, @PathVariable Integer delayTime) {
        rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUTING_KEY, message,
                correlationData -> {
                    correlationData.getMessageProperties().setDelay(delayTime);
                    return correlationData;
                });
        System.out.println("当前时间: " + new Date().toString() + "," + "发送一条延迟信息 :" + message);

    }




}

